<?php

define('MAX_USERNAME_LENGTH', 32);
define('MAX_PASSWORD_LENGTH', 64);

class AuthController extends Zend_Controller_Action 
{
	
	function init()
	{
	    $this->registry = Zend_Registry::getInstance();
	    $this->view = $this->registry['view'];
	    $this->view->baseUrl = $this->_request->getBaseUrl();
	    $this->view->errMsg = '';
//	    
//		// 验证cookie中的用户名密码正确性
//		if ($_COOKIE)
//		{
//			$authUser = new AuthUser();
//		}
//		global $USER_INFO;
//		var_dump($USER_INFO);
	}

	public function loginAction()
	{
		if ($_POST && count($_POST) > 1)
		{
			if (isset($_REQUEST['username_input']) && isset($_REQUEST['password_input']))
			{
				$username = $_REQUEST['username_input'];
				$password = $_REQUEST['password_input'];
				$authUser = new AuthUser();
				$this->userinfo = $authUser->LoginUser($username, $password);
				if ($this->userinfo)
				{
					$fivechessUser = new FivechessUser();
//					$fivechessUser->AddUserInfo($this->userinfo['id'], $this->userinfo['username'], $this->userinfo['petname']);
//					$fivechessUser->AddDraw($username);
//					echo $username, $password;
					echo $this->view->render('index.html');
					return true;
					
				}
			}
		}
		$this->view->errMsg = '登陆失败，请您输入正确的用户名及密码。';
		echo $this->view->render('auth/login.html');

	}
	
	public function regAction()
	{
		if ($_POST && count($_POST) > 1)
		{
			if (isset($_REQUEST['username_input']) && 
				isset($_REQUEST['password_input']) &&
				isset($_REQUEST['password2_input']))
			{
				
				$username = $_REQUEST['username_input'];
				$password = $_REQUEST['password_input'];
				$password2 = $_REQUEST['password2_input'];
//				echo $username, $password, $password2;
				if ($username && strlen($username) <= MAX_USERNAME_LENGTH && 
					$password && strlen($password) <= MAX_PASSWORD_LENGTH && $password == $password2)
				{
					$authUser = new AuthUser();
					$retCode = $authUser->RegUser($username, $password);
					if ($retCode)	# 注册成功
					{
//						global $USER_INFO;
//						echo "<br/>";
//						var_dump($USER_INFO);
						
						echo $this->view->render('index.html');//显示模版  
						return true;
					}
					else
					{
						$this->view->errMsg = '注册失败，该用户名已存在，请您尝试其他用户名。';
//						echo $this->view->render('auth/reg.html');
					}
					
				}
				else
				{
					$this->view->errMsg = '用户名密码不符合条件';
//					echo $this->view->render('auth/reg.html');
				}
			}
			else
			{
				$this->view->errMsg = '请输入用户名和密码';
//				echo $this->view->render('auth/reg.html');
			}
		}
		else
		{
			$this->view->errMsg = '';
//			echo $this->view->render('auth/reg.html');
		}
		echo $this->view->render('auth/reg.html');
	}
}






